package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.exception.SupermarketException;
import cn.outsourcing.supermarket.converter.HiolabsSpecificationConverter;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageSpecificationSaveDTO;
import cn.outsourcing.supermarket.mapper.HiolabsSpecificationMapper;
import cn.outsourcing.supermarket.po.HiolabsSpecification;
import cn.outsourcing.supermarket.service.BackstageSpecificationService;
import cn.outsourcing.supermarket.service.HiolabsSpecificationService;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsSpecificationVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gzkemays
 * @since 2021/11/4 16:24
 */
@Service
public class BackstageSpecificationServiceImpl implements BackstageSpecificationService {
  @Resource HiolabsSpecificationMapper specificationMapper;
  @Resource HiolabsSpecificationService specificationService;

  @Override
  public List<HiolabsSpecificationVO> getSpecification() {
    return HiolabsSpecificationConverter.INSTANCE.convertToVoList(
        specificationMapper.selectList(Wrappers.emptyWrapper()));
  }

  @Override
  public HiolabsSpecificationVO getDetail(OnlyIdPayloadDTO dto) {
    return HiolabsSpecificationConverter.INSTANCE.convertToVo(
        specificationMapper.selectById(dto.getId()));
  }

  @Override
  public Integer save(BackstageSpecificationSaveDTO dto) {
    HiolabsSpecification specification = new HiolabsSpecification();
    if (dto.getId() != null && dto.getId() != 0) {
      specification.setId(dto.getId());
    }
    specification.setSortOrder(dto.getSortOrder());
    specification.setName(dto.getName());
    if (specificationService.saveOrUpdate(specification)) {
      return dto.getId();
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "操作失败");
  }

  @Override
  public Integer destroy(OnlyIdPayloadDTO dto) {
    return specificationMapper.deleteById(dto.getId());
  }
}
